πŸŽƒ ggplot2 + plotly πŸŽƒ

Mostramos como usar plotly a partir de una grÑfica de ggplot2, ademÑs veremos como agregar emojis con el paquete emo 🦹

  1. 🧟 Cargamos los paquetes
library(tidyverse)
library(plotly)
library(emo)
  1. πŸ‘» Leemos los datos
movies <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-10-23/movie_profit.csv')
  1. πŸ§› Limpiamos los datos
# creamos una tabla que contenga ΓΊnicamente las pelΓ­culas de terror con
# ganancia y presupuesto positivos
# creamos ademΓ‘s una variable de aΓ±o en que se estrenΓ³
movies_horror <- movies %>%
  filter(genre == "Horror", worldwide_gross > 0, production_budget > 0) %>%
  mutate(year = as.numeric(str_sub(release_date, start = -1L - 3, end = -1L)))
  1. πŸ‘Ώ Primero un ggplot2
gg_horror <- ggplot(movies_horror, aes(x = production_budget,
                          y = worldwide_gross, color = year)) +
  geom_point() +
  theme_minimal() +
  scale_x_log10() +
  scale_y_log10() +
  facet_wrap(~ mpaa_rating)
gg_horror

  1. πŸ§™ plotly! creamos un grΓ‘fico interactivo con la funciΓ³n ggplotly
ggplotly(gg_horror)
  1. πŸ‘Ή Halloween? usaremos el paquete emo para incluir emojis en los datos
# Creamos una columna de emojis con el paquete emojifont!
movies_horror <- movies_horror %>% 
  mutate(figure = case_when(
    mpaa_rating == "PG" ~ "jack_o_lantern",
    mpaa_rating == "PG-13" ~ "ghost",
    mpaa_rating == "R" ~ "skull",
    TRUE ~ "imp"),
    emoji = map_chr(figure, emo::ji))
glimpse(movies_horror)
## Rows: 295
## Columns: 12
## $ ...1              <dbl> 43, 67, 233, 272, 287, 349, 386, 429, 452, 454, 475,…
## $ release_date      <chr> "12/14/2007", "2/12/2010", "5/19/2017", "8/4/2000", …
## $ movie             <chr> "I am Legend", "The Wolfman", "Alien: Covenant", "Ho…
## $ production_budget <dbl> 1.50e+08, 1.50e+08, 9.70e+07, 9.00e+07, 8.70e+07, 8.…
## $ domestic_gross    <dbl> 256393010, 62189884, 74262031, 73209340, 165092266, …
## $ worldwide_gross   <dbl> 585532684, 142634358, 238521247, 191200000, 35010028…
## $ distributor       <chr> "WarnerΒ Bros.", "Universal", "20thΒ CenturyΒ Fox", "So…
## $ mpaa_rating       <chr> "PG-13", "R", "R", "R", "R", "PG-13", "R", "PG-13", …
## $ genre             <chr> "Horror", "Horror", "Horror", "Horror", "Horror", "H…
## $ year              <dbl> 2007, 2010, 2017, 2000, 2001, 1999, 2004, 1998, 1999…
## $ figure            <chr> "ghost", "skull", "skull", "skull", "skull", "ghost"…
## $ emoji             <chr> "πŸ‘»", "πŸ’€", "πŸ’€", "πŸ’€", "πŸ’€", "πŸ‘»", "πŸ’€", "πŸ‘»", "πŸ’€"…
  1. πŸ’€ usamos ggplotly eligiendo que informaciΓ³n queremos desplegar
gg_horror_emo <- ggplot(movies_horror, aes(text = emoji, 
                                           x = production_budget, 
                                           y = worldwide_gross, color = year, 
                                           label = movie)) +
  geom_point() +
  theme_minimal() +
  scale_x_log10() +
  scale_y_log10() +
  facet_wrap(~ mpaa_rating)

ggplotly(gg_horror_emo, tooltip = c("emoji", "movie", "year"))